package control;



import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.Sprite;

import main.Res;


public class Button extends Control{
	protected Sprite sprite;
	protected String text;
	int extendX=0;

	protected boolean enable = true;
	protected boolean isTogled = false;

	int x,y;
	protected int color;
	
	public Button(int id,int x,int y) {
		sprite = new Sprite(Res.IMG_BTN_ICON, 32, 32);
		sprite.setFrame(id);
		sprite.setPosition(x, y);
		this.x = x;
		this.y = y;
		color = 0xff333333;
	}
	public Button(int id,int x,int y,int extendX) {
		sprite = new Sprite(Res.IMG_BTN_ICON, 32, 32);
		sprite.setFrame(id);
		sprite.setPosition(x, y);
		this.x = x;
		this.y = y;
		this.extendX = extendX;
		color = 0xff333333;
	}
	public String getText() {
		return text;
	}
	public void setText(String text) {
		this.text = text;
	}
	public boolean isTogled() {
		return isTogled;
	}

	public void setTogled(boolean isTogled) {
		this.isTogled = isTogled;
	}
	public int getColor() {
		return color;
	}
	public void setColor(int color) {
		this.color = color;
	}
	
	public void setEnable(boolean enable){
		this.enable = enable;
		if(enable){
			sprite.setFrame(0);
		}else{
			sprite.setFrame(1);
		}
	}
	protected boolean isHover(int x,int y)
	{
		return (x>sprite.getX()
				&&y>sprite.getY()
				&&x<sprite.getX()+sprite.getWidth()+extendX
				&&y<sprite.getY()+sprite.getHeight());
	}
	public boolean pointerPressed(int x,int y)
	{
		if(!enable){
			return false;
		}
		if(isHover(x,y))
		{
			sprite.setPosition(this.x+2, this.y+2);
			return true;
		}
		sprite.setPosition(this.x, this.y);
		return false;
	}
	
	public boolean pointerReleased(int x,int y)
	{
		if(!enable){
			return false;
		}
		sprite.setPosition(this.x, this.y);
		if(isHover(x,y))
		{
			if(command!= null)
			{
				command.Do();
			}
			return true;
		}
		return false;
	}
	protected void controlPaint(Graphics g)
	{
		if(isTogled && color != 0){
			g.setColor(color);
			//g.fillRect(x, y, sprite.getWidth(), sprite.getHeight());
			g.fillRoundRect(x, y, sprite.getWidth(), sprite.getHeight(),12,12);
		}
		sprite.paint(g);
		
		
		/*
		g.drawString(text, sprite.getX()+sprite.getWidth()/2+1, sprite.getY()+7,
				Graphics.TOP | Graphics.HCENTER);
		if(enable){
			g.setColor(0xffcc00);
			g.drawString(text, sprite.getX()+sprite.getWidth()/2, sprite.getY()+8,
				Graphics.TOP | Graphics.HCENTER);
		}*/
	}
}
